/*
 * @description: 
 * @Author: wzg
 * @Date: 2021-03-08 08:42:12
 * @LastEditTime: 2021-03-18 10:17:54
 */
/* 统一处理返回的数据格式 */
/* 在上下文对象中增加方法 */
export function result (option = {}) {
  return async (ctx, next) => {

    /* 返回对象 */
    ctx.success = (code, msg, data) => {
      ctx.type = option.type || 'application/json'
      // ctx.set("Content-Type", "application/json")
      ctx.body = {
        code: code || 0,
        msg: msg || '操作成功',
        result: data
      }
    }
    /* 返回所有数据 */
    ctx.successList = (code, msg, list) => {
      ctx.type = option.type || 'application/json'
      ctx.body = {
        code: code || 0,
        msg: msg || '操作成功',
        result: list
      }
    }
    /* 返回分页数组，包含 总数 */
    ctx.successPaginationList = (code, msg, list, totalCount) => {
      ctx.type = option.type || 'application/json'
      ctx.body = {
        code: code || 0,
        msg: msg || '操作成功',
        result: {
          list,
          totalCount
        }
      }
    }
    /* 错误返回数据 */
    ctx.error = (code, msg) => {
      ctx.type = option.type || 'application/json'
      ctx.body = {
        code: code,
        msg: msg,
      }
    }

    await next();
  }
}
// export default {
//   result
// }